package com.windfone.web.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.common.Constant;
import com.windfone.common.PageUtil;
import com.windfone.model.Promotions;
import com.windfone.service.PromotionsService;

public class PromotionsAction extends ActionSupport {
	private PromotionsService  promotionsService;
	private Promotions promotions;
	private int beginRecord;
	private int currentPage;
	private int nextPage;
	private int totalPage;
	private int prevPage;
	private List<Promotions> promotionsList;
	private int userId;
	private String title;
	private String content;
	private int promotionsId;
	public int getPromotionsId() {
		return promotionsId;
	}
	public void setPromotionsId(int promotionsId) {
		this.promotionsId = promotionsId;
	}
	public void setPromotionsService(PromotionsService promotionsService) {
		this.promotionsService = promotionsService;
	}
	public PromotionsService getPromotionsService() {
		return promotionsService;
	}
	public Promotions getPromotions() {
		return promotions;
	}
	public int getBeginRecord() {
		return beginRecord;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public int getNextPage() {
		return nextPage;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public int getPrevPage() {
		return prevPage;
	}
	public List<Promotions> getPromotionsList() {
		return promotionsList;
	}
	public int getUserId() {
		return userId;
	}
	public String getTitle() {
		return title;
	}
	public String getContent() {
		return content;
	}
	public void setPromotions(Promotions promotions) {
		this.promotions = promotions;
	}
	public void setBeginRecord(int beginRecord) {
		this.beginRecord = beginRecord;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}
	public void setPromotionsList(List<Promotions> promotionsList) {
		this.promotionsList = promotionsList;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public void setContent(String content) {
		this.content = content;
	}
	
	
	public String promotions() throws Exception {
		return this.SUCCESS;
	}
	public String addPromotions() throws Exception {
		Promotions bean = new Promotions();
		bean.setContent(content);
		bean.setTitle(title);
		bean.setUserId(userId);
		promotionsService.insertPromotions(bean);
		return this.SUCCESS;
	}
	public String deletePromotions() throws Exception {
		promotionsService.deletePromotions(promotionsId);
		return this.SUCCESS;
	}
	public String promotionsList() throws Exception {
		PageUtil pu = new PageUtil();
		pu.setCurrentPage(currentPage);
		int totalRecords = promotionsService.getAllRecorderNumber() ;
		pu.setTotalRecords( totalRecords );
		pu.setPageSize(Constant.PAGE_SIZE);
		pu.dealPage();
		currentPage = pu.getCurrentPage();
		nextPage = pu.getNextPage();
		totalPage = pu.getTotalPage();
		prevPage = pu.getPrevPage();
		
		beginRecord = Constant.PAGE_SIZE * (currentPage -1);
		if ( beginRecord< 0 )
			beginRecord = 0; 
		promotionsList = promotionsService.getAllPromotions(beginRecord);
		return this.SUCCESS;
	}
	public String beforUpdatePromotions() throws Exception {
		promotions = promotionsService.getPromotionsById( promotionsId).get(0);
		return this.SUCCESS;
	}
	public String updatePromotions() throws Exception {
		Promotions bean = new Promotions();
		bean.setContent(content);
		bean.setTitle(title);
		bean.setUserId(userId);
		bean.setId(promotionsId);
		promotionsService.updatePromotions(bean);
		return this.SUCCESS;
	}
 
}
